// import service from '@/utils/request';
import service from '@/utils/anvil'
import { isPlainObject, forOwn, isArray, isNull, isUndefined } from 'lodash-es'
import { AnvilAxiosRequestConfig, AnvilExtentConfig } from '@/utils/anvil'

/**
 * post请求，后端接收需要RequestBody注解
 * @param {string} url  地址
 * @param {object} params 参数
 * @param {object} extCfg 扩展配置参数
 *                 {
 *                    {boolean} objectAsMap 对象以Map的形式传输，后台用Map对象接收
 *                    {boolean} security 是否加密
 *                    {boolean} noLoading 不使用默认的加载等待
 *                 }
 */
export function post(url: string, params?: object, extCfg?: AnvilExtentConfig) {
  let formData: FormData
  // FormData对象则直接赋值
  if (
    (extCfg && extCfg.security === true) ||
    (params && params.constructor && 'FormData' === params.constructor.name)
  ) {
    formData = params as FormData
  } else {
    // 其他对象则转换为FormData对象
    formData = new FormData()
    if (isPlainObject(params)) {
      forOwn(params, (v: any, k: string) => {
        appendToFormData(formData, k, v, extCfg && extCfg.objectAsMap)
      })
    }
  }

  return service.request({
    url: url,
    method: 'post',
    data: formData,
    extCfg: {
      objectAsMap: extCfg && extCfg.objectAsMap === true,
      security: extCfg && extCfg.security === true,
      noLoading: extCfg && extCfg.noLoading === true
    }
  })
}

/**
 * 将k,v添加到form表单中
 * @param {FormData} formData form表单值
 * @param {String} k 键
 * @param {String|Object} v 值
 * @param {boolean} objectAsMap 对象以Map的形式传输，后台用Map对象接收
 */
function appendToFormData(formData: FormData, k: string, v: any, objectAsMap?: boolean) {
  if (isArray(v)) {
    forOwn(v, (v1: any, k1: string) => {
      appendToFormData(formData, k + '[' + k1 + ']', v1, objectAsMap)
    })
  } else if (isPlainObject(v)) {
    forOwn(v, (v1: any, k1: string) => {
      if (objectAsMap === true) {
        appendToFormData(formData, k + '[' + k1 + ']', v1, objectAsMap)
      } else {
        appendToFormData(formData, k + '.' + k1, v1, objectAsMap)
      }
    })
  } else {
    if (
      !isNull(k) &&
      !isNull(v) &&
      !isUndefined(k) &&
      !isUndefined(v)
    ) {
      formData.append(k, v)
    }
  }
}

/**
 * get请求
 * @param {string} url  地址
 * @param {object} params  参数
 */
export function get(url: string, params?: object, extCfg?: AnvilExtentConfig) {
  return service.request({
    url: url,
    method: 'get',
    params: params,
    extCfg: extCfg
  })
}

/**
 *
 * @param {object} config
 */
export function ajax(config: AnvilAxiosRequestConfig) {
  let formData: FormData
  if (config.data && config.data.constructor && 'FormData' === config.data.constructor.name) {
    formData = config.data as FormData
  } else {
    // 其他对象则转换为FormData对象
    formData = new FormData()
    if (isPlainObject(config.data)) {
      const objectAsMap = config.extCfg && config.extCfg.objectAsMap === true
      forOwn(config.data, (v: string, k: any) => {
        appendToFormData(formData, k, v, objectAsMap || false)
      })
    }
  }
  config.data = formData
  return service.request(config)
}

/**
 * 本地接口formdata类型调用，baseURl置空
 * @param {object} config
 */
export function localAjax(config: AnvilAxiosRequestConfig) {
  let formData: FormData
  if (config.data && config.data.constructor && 'FormData' === config.data.constructor.name) {
    formData = config.data as FormData
  } else {
    // 其他对象则转换为FormData对象
    formData = new FormData()
    if (isPlainObject(config.data)) {
      const objectAsMap = config.extCfg && config.extCfg.objectAsMap === true
      forOwn(config.data, (v: string, k: any) => {
        appendToFormData(formData, k, v, objectAsMap || false)
      })
    }
  }
  config.data = formData
  config.baseURL = ''
  return service.request(config)
}

/**
 * 本地接口json类型调用，baseURl置空
 * @param {object} config
 */
export function localAjaxByJson(config: AnvilAxiosRequestConfig) {
  config.baseURL = ''
  return service.request(config)
}

const anvilAjax = {
  post,
  get,
  ajax,
  localAjax,
  localAjaxByJson
}

export default anvilAjax
